<template>
  <!-- 首页 热门mv组件 -->
  <div>
    <!-- 定义插槽 -->
    <slot name="title"></slot>

    <!-- 轮播图 -->
    <el-carousel
      :interval="4000"
      :indicator-position="screenWidth <= 768 ? 'none' : 'outside'"
      type="card"
      :height="screenWidth <= 768 ? '2rem' : '3.5rem'"
      @change="changeHandler"
    >
      <el-carousel-item
        :label="`${mv.name}-${mv.author}`"
        v-for="(mv, index) in mvDatas"
        :key="index"
      >
        <div
          @click.stop="clickHandler(index, mv)"
          :title="`${mv.name}-${mv.author}`"
          class="mv-icon"
        >
          <img :src="mv.icon" :alt="mv.name" />
          <div class="play-mask-icon">
            <img src="../../assets/index/play-mask.png" alt="" />
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>

    <!-- mv视频弹出层 -->
    <el-dialog
      :title="title"
      :before-close="closeHandler"
      :visible.sync="mvToast"
      :destroy-on-close="true"
      custom-class="toast-class"
      :width="screenWidth <= 768 ? '80%' : '50%'"
    >
      <video
        controls
        class="mv-class"
        src="https://jsai.312jiah.com/mv-demo.mp4"
        muted
        preload="metadata"
      ></video>
    </el-dialog>
  </div>
</template>

<script>
import mvData from "@/data/mv.data";
import { mapState } from "vuex";
export default {
  name: "MusicMv",
  computed: {
    /* 获取视口宽度 */
    ...mapState("music", ["screenWidth"]),

    /* 获取mv的数据 */
    mvDatas() {
      return mvData.mvData;
    },
  },
  data() {
    return {
      idx: 0,
      mvToast: false,
      title: "未知的MV",
    };
  },
  methods: {
    /* 关闭mv弹出层 */
    closeHandler(done) {
      this.title = "未知的MV";
      done();
    },
    /* 改变轮播图索引 */
    changeHandler(idx) {
      this.idx = idx;
    },
    /* 打开MV弹出层 */
    clickHandler(idx, mv) {
      if (this.idx === idx) {
        this.mvToast = true;
        this.title = `${mv.name}-${mv.author}`;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.mv-class {
  width: 100%;
}
.el-carousel {
  .el-carousel__container {
    .el-carousel__item {
      border-radius: 0.2rem;
      overflow: hidden;
      .mv-icon {
        width: 100%;
        height: 100%;
        border-radius: 0.2rem;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
          &:hover ~ .play-mask-icon {
            display: block;
          }
        }
      }
      .play-mask-icon {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 998;
        display: none;
        &:hover {
          display: block;
        }
      }
    }
  }
}
</style>